package com.zhilianbao.leyaogo.receiver;

import android.content.Context;

import com.bql.convenientlog.CLog;
import com.bql.utils.EventManager;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.xiaomi.mipush.sdk.MiPushCommandMessage;
import com.xiaomi.mipush.sdk.MiPushMessage;
import com.xiaomi.mipush.sdk.PushMessageReceiver;
import com.zhilianbao.leyaogo.app.LeYaoGoApplication;
import com.zhilianbao.leyaogo.events.PushEventCode;
import com.zhilianbao.leyaogo.utils.Utils;

import org.greenrobot.eventbus.EventBus;

/**
 * ClassName: MiPushMessageReceiver <br>
 * Description: 小米推送Receiver<br>
 * Author: Cyarie <br>
 * Created: 2016/8/23 10:44 <br>
 * Update Time：<br>
 * Update Description：<br>
 */
public class MiPushMessageReceiver extends PushMessageReceiver {

    public static final String TAG = "MiPush";

    /**
     * 接收服务器推送的透传消息
     *
     * @param context       上下文
     * @param miPushMessage 消息
     */
    @Override
    public void onReceivePassThroughMessage(Context context, MiPushMessage miPushMessage) {
        super.onReceivePassThroughMessage(context, miPushMessage);
        Utils.sendMiNotification(miPushMessage);
        CLog.i(TAG, "onReceivePassThroughMessage---" + "---miPushMessage=" + miPushMessage);
    }

    /**
     * 接收服务器推送的消息
     *
     * @param context       上下文
     * @param miPushMessage 消息
     */
    @Override
    public void onReceiveMessage(Context context, MiPushMessage miPushMessage) {
        super.onReceiveMessage(context, miPushMessage);
        CLog.i(TAG, "onReceiveMessage---" + "---miPushMessage=" + miPushMessage);
    }

    /**
     * 接收服务器推送的通知消息，消息到达客户端时触发，还可以接受应用在前台时不弹出通知的通知消息，消息封装在
     * MiPushMessage类中。在MIUI上，只有应用处于启动状态，或者自启动白名单中，才可以通过此方法接受到该消息
     *
     * @param context       上下文
     * @param miPushMessage 消息
     */
    @Override
    public void onNotificationMessageArrived(Context context, MiPushMessage miPushMessage) {
        super.onNotificationMessageArrived(context, miPushMessage);
        CLog.i(TAG, "onNotificationMessageArrived---" + "---miPushMessage=" + miPushMessage);
    }

    /**
     * 接收服务器推送的通知消息，用户点击后触发
     *
     * @param context       上下文
     * @param miPushMessage 消息
     */
    @Override
    public void onNotificationMessageClicked(Context context, MiPushMessage miPushMessage) {
        super.onNotificationMessageClicked(context, miPushMessage);
        CLog.i(TAG, "onNotificationMessageClicked---" + "---miPushMessage=" + miPushMessage);
        //        Utils.clickMiNotification(miPushMessage);
    }


    /**
     * 获取给服务器发送注册命令的结果
     *
     * @param context              上下文
     * @param miPushCommandMessage 结果
     */
    @Override
    public void onReceiveRegisterResult(Context context, MiPushCommandMessage miPushCommandMessage) {
        super.onReceiveRegisterResult(context, miPushCommandMessage);
        if (miPushCommandMessage.getResultCode() == 0) {
            MiPushClient.setAlias(LeYaoGoApplication.getContext(), Utils.getUserLoginInfo().getUserId() + "", null);
        }
        CLog.i(TAG, "onReceiveRegisterResult---" + "---MiPushCommandMessage=" + miPushCommandMessage);
    }

    /**
     * 当客户端向服务器发送注册push、设置alias、取消注册alias、订阅topic、取消订阅topic等等命令后，从服务器返回结果
     *
     * @param context              上下文
     * @param miPushCommandMessage 结果
     */
    @Override
    public void onCommandResult(Context context, MiPushCommandMessage miPushCommandMessage) {
        super.onCommandResult(context, miPushCommandMessage);
        CLog.i(TAG, "onCommandResult---" + "---MiPushCommandMessage=" + miPushCommandMessage);

        //设置别名成功回调
        if (miPushCommandMessage.getCommand().equals("set-alias") && miPushCommandMessage.getResultCode() == 0) {
            EventBus.getDefault().post(new EventManager(PushEventCode.MSG_CODE_MI_PUSH));
        }
    }
}
